﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Linq.Expressions;
using Dapper;
using OddUserManager.Model;
using OddUserManager.Data.IRepositories;

namespace OddUserManager.Data.Repositories
{
    //public class MenuRepository : BaseRepository<Menus, int>, IMenuRepository
    //{
    //    public MenuRepository(OddUserManagerDbContext dbContext) : base(dbContext)
    //    {
    //    }
    //}

    public class MenuRepository : DapperBaseRepository<Menus, int>, IMenuRepository
    {
       

        public MenuRepository()
        {
        }


        //public  Menus Get(int id)
        //{
        //    Menus t = null;
        //    using (var con = GetOpenConnection())
        //    {
        //        var sql = $"select * from {typeof(Menus).Name} where Id={id.ToString()}";
        //        t = con.Query<Menus>(sql).FirstOrDefault();
        //    }
        //    return t;
        //}


        //public IEnumerable<Menus> GetAllList()
        //{
        //    List<Menus> list = null;
        //    using (var con = GetOpenConnection())
        //    {
        //        var sql = $"select * from {typeof(Menus).Name}";
        //        list = con.Query<Menus>(sql, null, null, true, 3000, null).ToList();
        //    }
        //    return list;
        //}

        public bool InsertOrUpdate(Menus entity)
        {
            var t = Get(entity.Id);
            if (null == t)
            {
                return Insert(entity);
            }
            else
            {
                return Update(entity);
            }
        }

        public IEnumerable<Menus> LoadPageList(int startPage, int pageSize, out int rowCount, Func<Menus, bool> where, Func<Menus, object> order)
        {
            var result = GetList();
            if (where != null)
                result = result.Where(where);
            if (order != null)
                result = result.OrderBy(order);
            else
                result = result.OrderBy(m => m.Id);
            rowCount = result.Count();
            return result.Skip((startPage - 1) * pageSize).Take(pageSize);
        }
        private IEnumerable<Menus> GetList()
        {
            IEnumerable<Menus> list = null;
            using (var con = GetOpenConnection())
            {
                var sql = string.Format($"select * from {typeof(Menus).Name}");
                list = con.Query<Menus>(sql, null, null, true, 3000, null);
            }
            return list;
        }

    }


}
